package controller

import (
	"github.com/gin-gonic/gin"
	"strconv"
	"wish_go/src/main/com/ajie/common"
	"wish_go/src/main/com/ajie/dao"
	"wish_go/src/main/com/ajie/model"
	"wish_go/src/main/com/ajie/result"
	"wish_go/src/main/com/ajie/vo"
)

// @Tags 轮播图
// @Summary 新增轮播图
// @accept json
// @Produce json
// @Security ApiKeyAuth
// @Param sort body int true "排序"
// @Param url body string true "图片地址"
// @Success 200 {object} result.Result "{"code": 200, "message": "操作成功"}"
// @Failure 500 {object} result.Result "{ "code": 500, "message": "异常信息" }"
// @Router /carousel/insert [POST]
func InsertCarousel(req *gin.Context) {
	carousel := model.Carousel{}
	// 解析参数为实体json
	_ = req.BindJSON(&carousel)
	if len(carousel.Url) == 0 || carousel.Sort == 0 {
		result.Error(req, 500, "参数不能为空")
		return
	}
	user := UserInfo(req)
	if user.Id != 0 {
		carousel.AreaCode = user.AreaCode
		carousel.CreateName = user.Name
	}
	dao.InsertCarousel(carousel)
	result.Success(req, "轮播图添加成功")
}

// @Tags 轮播图
// @Summary 修改轮播图
// @accept json
// @Produce json
// @Security ApiKeyAuth
// @Param id body int true "唯一标识"
// @Param sort body int false "排序"
// @Param url body string false "图片地址"
// @Success 200 {object} result.Result "{"code": 200, "message": "操作成功"}"
// @Failure 500 {object} result.Result "{ "code": 500, "message": "异常信息" }"
// @Router /carousel/update [PUT]
func UpdateCarousel(req *gin.Context) {
	carousel := model.Carousel{}
	// 解析参数为实体json
	_ = req.BindJSON(&carousel)
	user := UserInfo(req)
	if user.Id != 0 {
		carousel.UpdateName = user.Name
	}
	dao.UpdateCarousel(carousel)
	result.Success(req, "更新成功")
}

// @Tags 轮播图
// @Summary 删除轮播图
// @Produce json
// @Security ApiKeyAuth
// @Param id path int true "唯一标识"
// @Success 200 {object} result.Result "{"code": 200, "message": "操作成功"}"
// @Failure 500 {object} result.Result "{ "code": 500, "message": "异常信息" }"
// @Router /carousel/delete [DELETE]
func DeleteCarousel(req *gin.Context) {
	id, _ := strconv.Atoi(req.Param("id"))
	// 解析参数为实体json
	if id == 0 {
		result.Error(req, 500, "id不能为空")
		return
	}
	carousel := model.Carousel{}
	carousel.Id = uint(id)
	dao.DeleteCarousel(carousel)
	result.Success(req, "轮播图删除成功")
}

type CarouselVo struct {
	model.Carousel
	vo.Pagination
}

// @Tags 轮播图
// @Summary 分页获取轮播图
// @Produce json
// @Security ApiKeyAuth
// @Param pageSize body int true "第几页"
// @Param pageNum body int true "页数大小"
// @Param sortName body string false "排序字段"
// @Success 200 {object} result.Result "{"code": 200, "message": "分页查询成功", "rows": [], "total": 0}"
// @Failure 500 {object} result.Result "{ "code": 500, "message": "异常信息" }"
// @Router /carousel/page [post]
func PageCarousel(req *gin.Context) {
	var cv CarouselVo
	if err := req.BindJSON(&cv); err != nil {
		result.Error(req, 500, "参数解析异常:"+err.Error())
		return
	}
	pagination := common.GeneratePagination(vo.Pagination{PageNum: cv.PageNum, PageSize: cv.PageSize, SortName: cv.SortName})
	user := UserInfo(req)
	carousel := model.Carousel{BaseModel: model.BaseModel{CreateName: user.Name}}
	rows, total, err := dao.PageCarousel(&carousel, &pagination)
	if err != nil {
		result.Error(req, 500, "查询轮播图失败")
		return
	}
	result.PageRsult(req, rows, total)
}

// @Tags 轮播图
// @Summary 首页轮播图
// @Produce json
// @Success 200 {object} result.Result "{"code": 200, "message": "操作成功"}"
// @Failure 500 {object} result.Result "{ "code": 500, "message": "异常信息" }"
// @Router /carousel/index [GET]
func Carousel(req *gin.Context) {
	rows, err := dao.Carousel()
	if err != nil {
		result.Error(req, 500, "查询轮播图失败")
		return
	}
	result.SuccessData(req, "轮播图获取成功", rows)
}
